package com.jinlia.show.modules.base.system.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.system.entity.GoodsAttr;
import com.jinlia.show.modules.base.system.entity.GoodsLabel;
import com.jinlia.show.modules.base.system.entity.GoodsLabelRel;
import com.jinlia.show.modules.base.system.mapper.GoodsLabelMapper;
import com.jinlia.show.modules.base.model.query.GoodsLabelQuery;
import com.jinlia.show.modules.base.model.po.GoodsPO;
import com.jinlia.show.modules.base.model.po.GoodsAttrPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GoodsLabelMapperService extends ServiceImpl<GoodsLabelMapper, GoodsLabel> {
    @Autowired
    GoodsAttrMapperService goodsSkuMapperService;
    @Autowired
    GoodsLabelRelMapperService goodsLabelRelMapperService;

    public List<GoodsPO> getGoodsPOsByGoodsTagId(Long... goodsTagId) {
        //获取GOODs
        //填充Goods sku信息
        return null;
    }

    public List<GoodsLabel> getGoodsTagsByGoodsId(Long... goodsId) {
        List<GoodsLabelRel> list = goodsLabelRelMapperService.lambdaQuery().in(GoodsLabelRel::getGoodsId, goodsId).list();
        if (!CollectionUtils.isEmpty(list)) {
            Long[] tagIds = list.stream().map(GoodsLabelRel::getLabelId).toArray(Long[]::new);
            List<GoodsLabel> listGoodsTag = this.lambdaQuery().in(GoodsLabel::getId, tagIds).list();
            return listGoodsTag;
        }
        return Arrays.asList();
    }

    public List<GoodsPO> getGoodsPO(GoodsLabelQuery goodsTagQuery) {
        List<GoodsPO> goodsPO = this.baseMapper.getGoodsPO(goodsTagQuery);
        Long[] goodsIds = goodsPO.stream().map(GoodsPO::getId).toArray(Long[]::new);
        Map<Long, List<GoodsAttrPO>> collect = goodsSkuMapperService.getPOsByGoodsId(goodsIds)
                .stream()
                .collect(Collectors.groupingBy(GoodsAttr::getGoodsId));
        goodsPO.forEach(i -> {
            if (collect.containsKey(i.getId())) {
                i.setGoodsAttrPOS(collect.get(i.getId()));
            }
        });
        return goodsPO;
    }

    public void bindRel(GoodsLabelRel goodsLabelRel) {
        /**
         * 校验唯一
         */
        goodsLabelRelMapperService.lambdaQuery()
                .eq(GoodsLabelRel::getGoodsId, goodsLabelRel.getGoodsId())
                .eq(GoodsLabelRel::getLabelId, goodsLabelRel.getLabelId())
                .oneOpt()
                .ifPresent(i -> {
                    throw new RuntimeException("标签已存在");
                });
        goodsLabelRelMapperService.save(goodsLabelRel);
    }

    public void bindRemove(GoodsLabelRel goodsLabelRel) {
        goodsLabelRelMapperService.lambdaUpdate()
                .eq(GoodsLabelRel::getGoodsId, goodsLabelRel.getGoodsId())
                .eq(GoodsLabelRel::getLabelId, goodsLabelRel.getLabelId())
                .remove();
    }
}
